<script setup lang="ts">
import { ref, provide, computed } from 'vue'
import { useRoute } from 'vue-router'
import Nav from './components/nav.vue'

const route = useRoute()

// 判断当前是否在方案生成页面
const isSchemePage = computed(() => route.path === '/create/libraryScheme')

// 创建一个响应式的 ref 来存储子组件的方法
const schemeMethods = ref<{
  handleCreateLibrary?: () => void
  handleAuditLibrary?: () => void
}>({})

// 只在方案生成页面提供方法注册
if (isSchemePage.value) {
  provide('registerSchemeMethods', (methods: {
    handleCreateLibrary?: () => void
    handleAuditLibrary?: () => void
  }) => {
    schemeMethods.value = methods
  })
}

const handleCreateLibrary = () => {
  if (isSchemePage.value) {
    schemeMethods.value?.handleCreateLibrary?.()
  } else {
    console.log('新建方案库 - 非方案生成页面')
  }
}

const handleAuditLibrary = () => {
  if (isSchemePage.value) {
    schemeMethods.value?.handleAuditLibrary?.()
  } else {
    console.log('待我审核 - 非方案生成页面')
  }
}
</script>

<template>
  <div class="h-full flex flex-col py-5 gap-4">
    <Nav @createLibrary="handleCreateLibrary" @auditLibrary="handleAuditLibrary" />
    <div class="flex-1">
      <router-view />
    </div>
  </div>
</template>

<style lang="less" scoped></style>
